﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.MerchantsSettings
{
    public class Order_MerPricingController : Controller
    {
        //
        // GET: /Order_MerPricing/

        public ActionResult Index()
        {
            return View();
        }

        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();

        /// <summary>
        /// 获取系统的增删查改的权限
        /// </summary>
        /// <returns></returns>
        public ActionResult getPower()
        {
            try
            {
                string[] PowerS = null;
                //string RetPower = "";
                Models.ControlPower RetPower = new Models.ControlPower();
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
                if (user != null)
                {
                    PowerS = user.RolePower.Split(',');//权限码
                    //拼接增删查改的权限
                    foreach (string pow in PowerS)
                    {
                        if (pow == "009009002")
                        {
                            RetPower.Add = 1;
                        }
                        else if (pow == "009009003")
                        {
                            RetPower.Dell = 1;
                        }
                        else if (pow == "009009004")
                        {
                            RetPower.Edit = 1;
                        }
                    }
                }
                else
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                return Json(RetPower);
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取字符渠道
        /// </summary>
        /// <param name="PricType"></param>
        /// <returns></returns>
        public ActionResult getOrderTPayType(int PricType, string PayID)
        {
            try
            {
                List<Order_MerPricing_PayInfo> list = new List<Order_MerPricing_PayInfo>();//定义支付渠道
                string result = service.GetOrder_PayType(PricType, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Order_MerPricing_PayInfo m = new Order_MerPricing_PayInfo();

                            m.PayID = item["PayID"].ToString();
                            m.PayName = item["PayName"].ToString();
                            if (m.PayID == PayID)
                            {
                                m.selected = true;
                            }
                            else
                            {
                                m.selected = false;
                            }
                            list.Add(m);
                        }
                    }
                    return Json(list, JsonRequestBehavior.AllowGet);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取当前操作员拥有商户的权限
        /// </summary>
        /// <returns></returns>
        public ActionResult getMer(JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.Order_Mer_T> page = new Page<Model.Order_Mer_T>();
                List<Model.Order_Mer_T> list = new List<Model.Order_Mer_T>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = service.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Model.Order_Mer_T m = new Model.Order_Mer_T();
                            m.MerID = item["MerID"].ToString();
                            m.MerName = item["MerName"].ToString();
                            m.MerAdr = item["MerAdr"].ToString();
                            m.MerCall = item["MerCall"].ToString();
                            m.MerTxt = item["MerTxt"].ToString();
                            list.Add(m);
                        }
                    }
                    jqgridparam.records = list.Count;//查询的数据总数
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = page.FindListPage(list, ref jqgridparam)//返回当前页的数据
                    };
                    return Json(data);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取所有优惠信息
        /// </summary>
        /// <param name="MerID"></param>
        /// <returns></returns>
        public List<View_Order_MerPricing> GetAllMerPricing(string MerID)
        {
            List<View_Order_MerPricing> list = new List<View_Order_MerPricing>();
            string result = service.GetOrder_MerPricing(1,10, MerID, GetYZM._YZM, 1);
            JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
            string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
            if (bs == "1")//返回结果正常
            {
                IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果 
                if (jieguo.Count > 0)
                {
                    foreach (JToken item in jieguo)
                    {
                        // List<Model.Sys_Component_T> list_com = new List<Model.Sys_Component_T>();
                        List<View_Order_MerPricing> list_Dishes = new List<View_Order_MerPricing>();
                        View_Order_MerPricing m = new View_Order_MerPricing();
                        m.PricID = item["PricID"].ToString();
                        m.MerID = item["MerID"].ToString();
                        m.IsOpen = int.Parse(item["IsOpen"].ToString());
                        m.PricType = int.Parse(item["PricType"].ToString());
                        m.MerID = item["MerID"].ToString();
                        m.PayID = item["PayID"].ToString();
                        m.PayName = item["PayName"].ToString();
                        m.IsFormMon = int.Parse(item["IsFormMon"].ToString());
                        m.IsPackMon = int.Parse(item["IsPackMon"].ToString());
                        m.IsMaxPric = DataHelper.MoneyToYuang(item["IsMaxPric"].ToString());
                        m.IsFirstMon = DataHelper.MoneyToYuang(item["IsFirstMon"].ToString());
                        m.MonType = int.Parse(item["MonType"].ToString());
                        if (m.MonType == 0)
                        {
                            m.PricMon = DataHelper.MoneyToYuang(item["PricMon"].ToString());
                        }
                        else
                        {
                            m.PricMon = item["PricMon"].ToString();
                        }
                        m.PricRem = int.Parse(item["PricRem"].ToString());
                        m.PricTxt = item["PricTxt"].ToString();
                        m.MerName = item["MerName"].ToString();
                        list.Add(m);
                    }
                }
                return list;
            }
            else
            {
                return list;
            }
        }


        /// <summary>
        /// 获取商户的优惠信息
        /// </summary>
        /// <param name="MerID">商户ID</param>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult getgrid(string MerID, JqGridParam jqgridparam)
        {
            try
            {
                Page<View_Order_MerPricing> page = new Page<View_Order_MerPricing>();//分页类定义
                List<View_Order_MerPricing> list = new List<View_Order_MerPricing>();

                string result = service.GetOrder_MerPricing(jqgridparam.page, jqgridparam.rows, MerID, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        //list = ParseClass<View_Order_MerPricing>.JsonList(jieguo);//解析返回的结果
                        foreach (JToken item in jieguo)
                        {
                            // List<Model.Sys_Component_T> list_com = new List<Model.Sys_Component_T>();
                            List<View_Order_MerPricing> list_Dishes = new List<View_Order_MerPricing>();
                            View_Order_MerPricing m = new View_Order_MerPricing();
                            m.PricID = item["PricID"].ToString();
                            m.MerID = item["MerID"].ToString();
                            m.IsOpen = int.Parse(item["IsOpen"].ToString());
                            m.PricType = int.Parse(item["PricType"].ToString());
                            m.MerID = item["MerID"].ToString();
                            m.PayID = item["PayID"].ToString();
                            m.PayName = item["PayName"].ToString();
                            m.IsFormMon = int.Parse(item["IsFormMon"].ToString());
                            m.IsPackMon = int.Parse(item["IsPackMon"].ToString());
                            m.IsMaxPric = DataHelper.MoneyToYuang(item["IsMaxPric"].ToString());
                            m.IsFirstMon = DataHelper.MoneyToYuang(item["IsFirstMon"].ToString());
                            m.MonType = int.Parse(item["MonType"].ToString());
                            if (m.MonType == 0)
                            {
                                m.PricMon = DataHelper.MoneyToYuang(item["PricMon"].ToString());
                            }
                            else
                            {
                                m.PricMon = item["PricMon"].ToString();//显示折扣优惠百分比
                            }
                            m.PricRem = int.Parse(item["PricRem"].ToString());
                            m.PricTxt = item["PricTxt"].ToString();
                            m.MerName = item["MerName"].ToString();
                            list.Add(m);
                        }
                    }
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = "0",
                        rows = list
                    };
                    return Json(data);
                }
            }
            catch (Exception ex)
            {
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 增、删、改商品优惠信息
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateOrder_MerPricing(int Tag, View_Order_MerPricing Order_MerPricing)
        {
            try
            {
                Model.Order_MerPricing_T New_MerPricing = new Model.Order_MerPricing_T();//传入订单优惠信息
                string result = "";
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                New_MerPricing.PricID = Order_MerPricing.PricID;
                New_MerPricing.MerID = Order_MerPricing.MerID;
                New_MerPricing.IsOpen = Order_MerPricing.IsOpen;
                New_MerPricing.PricType = Order_MerPricing.PricType;
                New_MerPricing.PayID = Order_MerPricing.PayID;
                New_MerPricing.IsFormMon = Order_MerPricing.IsFormMon;
                New_MerPricing.IsPackMon = Order_MerPricing.IsPackMon;
                New_MerPricing.IsMaxPric = int.Parse((double.Parse(Order_MerPricing.IsMaxPric)*100).ToString());//把金额格式转换为分
                New_MerPricing.IsFirstMon = int.Parse((double.Parse(Order_MerPricing.IsFirstMon) * 100).ToString());//把金额格式转换为分
                New_MerPricing.MonType = Order_MerPricing.MonType;
                if (Order_MerPricing.MonType == 1)
                {
                    New_MerPricing.PricMon = int.Parse((double.Parse(Order_MerPricing.PricMon) * 10).ToString());//把折扣格式转换为百分比
                }
                else
                {
                    New_MerPricing.PricMon = int.Parse((double.Parse(Order_MerPricing.PricMon) * 100).ToString());//把金额格式转换为分

                }
                New_MerPricing.PricRem = Order_MerPricing.PricRem;
                New_MerPricing.PricTxt = Order_MerPricing.PricTxt;
                string json = New_MerPricing.ToJson();
                result = service.SetOrder_MerPricing(user.OperID, json, Tag, GetYZM._YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception ex)
            {
                return Content("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 优惠信息
        /// </summary>
        public class View_Order_MerPricing
        {
            public string IsFirstMon { get; set; }
            public int IsFormMon { get; set; }
            public string IsMaxPric { get; set; }
            public int IsOpen { get; set; }
            public int IsPackMon { get; set; }
            public string MerID { get; set; }
            public string MerName { get; set; }
            public int MonType { get; set; }
            public string PayID { get; set; }
            public string PayName { get; set; }
            public string PricID { get; set; }
            public string PricMon { get; set; }
            public int PricRem { get; set; }
            public int PricState { get; set; }
            public string PricTxt { get; set; }
            public int PricType { get; set; }
        }

        /// <summary>
        /// 支付类型
        /// </summary>
        public class Order_MerPricing_PayInfo
        {
            public string PayID { get; set; }
            public string PayName { get; set; }
            public bool selected { get; set; }
        }
    }
}
